/**
 * 根据窗口大小设置默认数据条数
 */
export function getPaginationDefaultSize(): 40 | 30 | 20 | 15 {
    const height = window.innerHeight
    if (height > 1500) {
        return 40
    } else if (height > 1200) {
        return 30;
    } else if (height > 1000) {
        return 20;
    }
    return 15
}


/**
 * 生成一个随机字符串key
 * @returns {string}
 */
export function CreateKey(): string {
    let str: string = '';
    for (let i = 0; i < 3; i++) {
        str += (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
    }
    if (str.substring(0, 1) === '0') {
        str = 'O' + str.substring(1, str.length);
    }
    return str.toUpperCase();
}


/**
 * 插入数据
 * @param value
 */
export function copy(value: string):Promise<boolean> {
    return new Promise(resolve => {
        if (typeof navigator?.clipboard?.writeText === 'function') {
            navigator.clipboard.writeText(value).then(() => resolve(true)).catch(() => resolve(false))
        } else {
            const textarea: HTMLTextAreaElement = document.createElement('textarea');
            textarea.value = value;
            document.body.appendChild(textarea);
            textarea.select();
            document.execCommand('copy');
            document.body.removeChild(textarea);
            resolve(true)
        }
    })
}


/**
 * 毫秒定时器,和setInterval一样的用法,但是会立即执行一次
 * @param handler
 * @param timeout
 */
export function runInterval(handler: () => void, timeout?: number): number {
    handler();
    return setInterval(handler, timeout);
}